package com.jaj.services.rap;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jaj.common.HttpUtils;
import com.jaj.models.RapProductLine;
import org.apache.http.Header;
import org.apache.http.message.BasicHeader;

/**
 * Created by chm on 2018/1/25.
 *
 * @author j aj
 */
public class ProjectLineService {

    /**
     * 删除产品线
     * @param sessionId 用户SessionId
     * @param productLineId 产品线Id
     * @return 结果
     */
    public static boolean deleteProductLine(String sessionId,String productLineId){
        String url = RapConfig.Url.ProductLine.Delete;
        //拼装参数
        Map<String, String> param = new HashMap<>();
        param.put("id", productLineId);
        //拼接Cookie（包含登录信息）
        List<Header> headerList = new ArrayList<>();
        Header header = new BasicHeader("Cookie", "JSESSIONID=" + sessionId);
        headerList.add(header);
        try {
            String httpReturn = HttpUtils.httpClientPost(url, param, headerList);
            JSONObject jsonObject = JSON.parseObject(httpReturn);
            if (jsonObject.getInteger("code") == 200) {
                return true;
            }
            return false;
        }
        catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 创建产品线.
     * @param sessionId 用户SessionId
     * @param corpId 团队Id
     * @param name 产品线名字
     * @return 产品线ID
     */
    public static String createProductLine(String sessionId,String corpId,String name){
        String url = RapConfig.Url.ProductLine.Create;
        //拼装参数
        Map<String, String> param = new HashMap<>();
        param.put("corpId", corpId);
        param.put("name", name);
        //拼接Cookie（包含登录信息）
        List<Header> headerList = new ArrayList<>();
        Header header = new BasicHeader("Cookie", "JSESSIONID=" + sessionId);
        headerList.add(header);
        try {
            String httpReturn = HttpUtils.httpClientPost(url, param, headerList);
            JSONObject jsonObject = JSON.parseObject(httpReturn);
            JSONObject jbProLine= (JSONObject) jsonObject.getJSONArray("items").get(0);
            return jbProLine.getString("id");
        }
        catch (Exception e) {
            e.printStackTrace();
            return e.getMessage();
        }
    }

    /**
     * 获取所有的产品线
     * @param sessionId 用户SessionId
     * @param teamId 团队Id
     * @return 产品线列表
     */
    public static List<RapProductLine> getAllProductLine(String sessionId, String teamId){
        String url = RapConfig.Url.ProductLine.GetAll+"?corpId="+teamId;
        List<Header> headerList = new ArrayList<>();
        Header header = new BasicHeader("Cookie", "JSESSIONID=" + sessionId);
        headerList.add(header);
        try {
            String result= HttpUtils.httpClientGet(url,headerList);
            JSONObject jsonObject=JSON.parseObject(result);
            if(jsonObject!=null){
                List<RapProductLine> resultList= new ArrayList<>();
                for (Object object : jsonObject.getJSONArray("items")) {
                    JSONObject proLine=(JSONObject)object;
                    RapProductLine productLine= new RapProductLine();
                    productLine.setCount(proLine.getString("count"));
                    productLine.setId(proLine.getString("id"));
                    productLine.setName(proLine.getString("name"));
                    resultList.add(productLine);
                }
                return resultList;
            }
            return null;
        }
        catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static void main(String[] args) {
        String sessionId = LoginService.rapLogin("", "");
//        String plid= createProductLine(sessionId,RapConfig.Team.ShareTeamId,"意见反馈与评论");
//        System.err.println(plid);
//        List<RapProductLine> jsonArray= getAllProductLine(sessionId,RapConfig.Team.ShareTeamId);
//        System.err.println(JSON.toJSONString(jsonArray));
        boolean bl=deleteProductLine(sessionId,"88");
    }
}
